package com.hnqcgc.dao;

import com.hnqcgc.db.MyTransaction;
import com.hnqcgc.vo.TProduct;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements IProductDao{
    MyTransaction myThansaction;public ProductDaoImpl (MyTransaction myThansaction){
        this.myThansaction =myThansaction;
    }

    @Override
    public List<TProduct> getAnyProduct(int count) throws SQLException {
        //得到数据库连接
        Connection connection = myThansaction.getConnection();
        String sql="select * from t_product limit ?";
        //向数据库发送sql语句
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,count);//给sql语句中的？注入值
        //执行sql语句
        ResultSet rs =ps.executeQuery();
        //准备一个集合类对象，装数据
        List<TProduct> list=new ArrayList<>();
        while (rs.next()){
            TProduct product=new TProduct();
            product.setTpId(rs.getInt(1));
            product.setTpName(rs.getString(2));
            product.setTpDescription(rs.getString(3));
            product.setTpPrice(rs.getDouble(4));
            product.setTpStock(rs.getInt(5));
            product.setTpcId(rs.getInt(6));
            product.setTpcChildId(rs.getInt(7));
            product.setTpFileName(rs.getString(8));

            list.add(product);
        }
        rs.close();
        ps.close();
        return list;

    }

    @Override
    public TProduct getProductInfoById(int pid) throws SQLException {
        //得到数据库连接
        Connection connection = myThansaction.getConnection();
        String sql="select * from t_product where tp_id=?";
        //PreparedStatement向数据库发送sql语句
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,pid);//给sql语句中的？注入值
        //执行sql语句
        ResultSet rs =ps.executeQuery();
        TProduct product=new TProduct();
        //将结果集中的数据存放在对象中
        if (rs.next()){
            product.setTpId(rs.getInt(1));
            product.setTpName(rs.getString(2));
            product.setTpDescription(rs.getString(3));
            product.setTpPrice(rs.getDouble(4));
            product.setTpStock(rs.getInt(5));
            product.setTpcId(rs.getInt(6));
            product.setTpcChildId(rs.getInt(7));
            product.setTpFileName(rs.getString(8));
        }
        //释放资源
        rs.close();
        ps.close();
        return product;
    }

    public static void main(String[] args) {
        MyTransaction thansaction=new MyTransaction(true);
        IProductDao productDao=new ProductDaoImpl(thansaction);
        try {
//            List<TProduct> list=productDao.getAnyProduct(8);
//            for (TProduct tProduct : list) {
//                System.out.println(tProduct.toString());
//            }
            TProduct p = productDao.getProductInfoById(5);
            System.out.println(p);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}